<template>
	<view>
		<view class="header-title">
			<view>注册成功，欢迎您的到来!</view>
		</view>
		<button class="bts" open-type="getUserInfo" @getuserinfo="Login">绑定微信进入系统</button>
	</view>
</template>

<script>
	import api from "@/common/api.js";
	export default {
		data() {
			return {
				username: '',
				phone: '',
				pass: '',
				id: '',
				cheng: ''
			}
		},
		methods: {
			register(data) {
				let _this = this
				api.zhuce(data).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.datef,
							icon: 'none',
							duration: 2000,
							success: () => {
								setTimeout(() => {
									uni.reLaunch({
										url: '../../index/index'
									})
									uni.setStorageSync('message', res.date)
								}, 2000)
							}
						})
					} else if (res.code == 203) {
						uni.showToast({
							title: '微信已绑定其他手机号',
							icon: 'none',
							duration: 2000,
							success: () => {
								setTimeout(() => {
									uni.reLaunch({
										url: '../../chatLogin/chatLogin'
									})
								}, 2000)
							}
						})
					}
				}).catch(err => {
					console.log(err)
				})
			},
			Login(e) {
				let that = this
				e.detail.userInfo.gender === 1 ? this.gender = "男" : this.gender = '女'
				if (e.detail.userInfo) {
					uni.login({
						success: (res) => {
							that.code = res.code
							uni.getUserInfo({
								success(res) {
									that.register({
										api: 'namezhu',
										wx: that.code,
										signature: res.signature,
										rawData: res.rawData,
										encryptedData: res.encryptedData,
										iv: res.iv,
										cheng: that.cheng,
										sex: that.gender,
										id: that.id
									})
								}
							})
						}
					})
				} else {
					uni.showToast({
						title: "您点击了拒绝授权",
						icon: 'none',
						duration: 1000
					})
				}
			}
		},
		onShareAppMessage(options) {
			let shareObj = {
				success: (res) => {
					if (res.errMsg == 'shareAppMessage:ok') {
						uni.showToast({
							title: '分享成功',
							icon: 'success',
							duration: 1500
						})
					}
				},
				fail: () => {
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						uni.showToast({
							title: '取消分享',
							icon: 'none',
							duration: 2000
						})
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				},
				complete: () => {
					// 转发结束之后的回调（转发成不成功都会执行）
				}
			}
		},
		onLoad(options) {
			this.username = options.name
			this.phone = options.phone
			this.pass = options.pass
			this.id = options.id
			uni.request({ // 获取ip
				url: 'http://ip-api.com/json/?lang=zh-CN',
				success: (e) => {
					console.log(e)
					this.ip = e.data.query
					this.cheng = e.data.country + '-' + e.data.regionName + '-' + e.data.city
				}
			})
		}
	}
</script>

<style lang="scss">
	.header-title {
		margin-top: 200rpx;
		color: #333;
		font-size: 30rpx;

		view {
			text-align: center;
			line-height: 60rpx;
		}
	}

	.bts {
		width: 690rpx;
		margin: 50rpx auto 0;
		height: 100rpx;
		line-height: 100rpx;
		border-radius: 50rpx;
		text-align: center;
		color: #fff;
		font-size: 36rpx;
		background: #209FFC;
	}
</style>
